import app from './app'

const wx = require('weixin-js-sdk')
/* 判断是否在微信环境 */
function is_weixn(){
  var ua = navigator.userAgent.toLowerCase();
  if(ua.match(/MicroMessenger/i)=="micromessenger") {
    return true;
  } else {
    return false;
  }
}

const common = {
  // 检查登陆
  checkLogin () {
    app.checkLogin()
  },
  // 调起登陆,跳转到App登陆页
  onLogin () {
    app.login({ 'reload': 1,'type': 0 })
  },
  // 跳转至App商品详情页
  jump2goods (id) {
    if(is_weixn()){
      wx.miniProgram.getEnv(function (res) {
        if (res.miniprogram) {
          wx.miniProgram.navigateTo({url: '/pages/home/goodsDetail/goodsDetail?linkId=' + id})
        }
      })
    }else{
      app.goods({goodsId: id, 'specialId': ''})
    }
  },
  // 跳转至App专区页
  jump2special (id) {
    if(is_weixn()){
      wx.miniProgram.getEnv(function (res) {
        if (res.miniprogram) {
          wx.miniProgram.navigateTo({url: '/pages/home/zone/zone?linkId=' + id})
        }
      })
    }else{
      app.special({ goodsId: '', specialId: id})
    }
  },
  share () {
    let shareTitle = arguments[0]
    let obj = arguments[1]
    let imgurl = arguments[2]
    let url
    if (window.location.href.includes('os=')) {
      url = window.location.href.replace('os=', 'isshare=1&os=')
    } else {
      if (window.location.href.includes('.html?')) {
        url = window.location.href.replace('.html?', '.html?isshare=1&')
      } else {
        url = window.location.href + '?isshare=1'
      }
    }
    console.log(url)
    app.share({
      'shareTitle': shareTitle,
      'shareContent': obj || '活动正在进行中',
      'imgUrl': imgurl || 'https://static.shandjj.com/website/default/images/logos_bif.png',
      'activeUrl': url,
      'showType': 0,
      'shareConf': {
        'shareData': [
          {'type': 1,view: 1},
          {'type': 2,view: 1},
          {'type': 3,view: 1},
          {'type': 4,view: 1},
          {'type': 5,view: 1},
          {'type': 6, view: 1}
        ]
      }
    })
  },
  shareMiniProgram () {
    let shareTitle = arguments[0]
    let obj = arguments[1]
    let imgurl = arguments[2]
    let url,linkId;
    if (window.location.href.includes('os=')) {
      url = window.location.href.replace('os=', 'isshare=1&os=')
    } else {
      if (window.location.href.includes('.html?')) {
        url = window.location.href.replace('.html?', '.html?isshare=1&');
      } else {
        url = window.location.href + '?isshare=1';
      }
    };
    linkId = "/pages/webpage/webpage?linkId="+url;
    console.log('linkId'+linkId)
    app.share({
      'shareTitle': shareTitle,
      'shareContent': obj || '活动正在进行中',
      'imgUrl': imgurl || 'https://static.shandjj.com/website/default/images/logos_bif.png',
      'activeUrl': url,
      'showType': 0,
      "wxMiniPath":linkId,
      'shareConf': {
        'shareData': [
          {'type': 1,view: 1},
          {'type': 2,view: 1},
          {'type': 3,view: 1},
          {'type': 4,view: 1},
          {'type': 5,view: 1},
          {'type': 6, view: 1}
        ]
      }
    })
  },
  // 跳转地址
  jump2url (url) {
    if(is_weixn()){
      wx.miniProgram.getEnv(function (res) {
        if (res.miniprogram) {
          wx.miniProgram.navigateTo({url:"/pages/webpage/webpage?linkId="+url})
        }
      })
    }else{
      app.openWeb({ url })
    }
  },
  closeWeb(){
    app.close()
  },
  jumpHome () {
    app.backHome()
  }
}
export default common
